import random
from common import token
from testdata import tyturl
from datetime import datetime, timedelta

class production_userinfo:

    userinfo = {
        "userNo": "212001",
        "password": "724b9321f3a05ea849d56c3d664e82f3"
    }

class production_header:
    header = {
        "Cookie": "TYT_Token=" + token.readToken(),
        "Content-Type": "application/json"
    }

# ----------------------------------getter----------------------------------

def getUserinfo(key):
    # 获得账号密码，不存在则提示读取对应变量失败
    try:
        return production_userinfo().userinfo[key]
    except Exception as e:
        raise Exception("获取的字典key不存在，错误提示：", e)


def getUrl(key):
    # 获得url，不存在则提示读取对应变量失败
    try:
        return tyturl.production_url().url[key]
    except Exception as e:
        raise Exception("目标地址不存在。错误提示：", e)


def getHeader():
    # 获得请求头，不存在则提示读取对应变量失败
    try:
        return production_header().header
    except Exception as e:
        raise Exception("请求头错误。错误提示：", e)


# 为自定义区间生成起始时间
def get_random_dates():
    """
    随机生成两个过去的日期，前者早于后者
    返回格式：yyyy-mm-dd，时间范围在8年内
    """
    today = datetime.now()
    # 随机生成两个不同的天数（0到2920天之间，约8年）
    days1 = random.randint(1, 2920)
    days2 = random.randint(1, days1)

    date1 = (today - timedelta(days=days1)).strftime('%Y-%m-%d')  # 较早的日期
    date2 = (today - timedelta(days=days2)).strftime('%Y-%m-%d')  # 较晚的日期

    return date1, date2